When
value = 32912;
is executed
nothing new is created.
A pattern representing 32912 is stored in the bits of value
.
Notice that there is a difference between the two statements:
value = 32912;
and
str = new String( "The Gingham Dog" );
In the first statement, value
is a primitive type,
so the assigment statement puts the data directly into it.
In the second statement, str
is an object reference variable
(the only other possibility)
so a reference to the object is put into that variable.
There are only primitive variables and object reference variables, and each contains a specific kind of information:
Kind of Variable | Information it Contains | When on the left of "=" |
---|---|---|
primitive variable | Contains actual data. | Previous data is replaced with new data. |
reference variable | Contains information on how to find an object. | Old reference is replaced with a new reference |
How do you tell the two kinds of variables apart? Easy: look at how the variable is declared. Unless it was declared to be of a primitive type, it is an object reference variable. A variable will not change its declared type.